import Vue from 'vue';
import {
  RouterMount,
  createRouter
} from 'uni-simple-router';

const router = createRouter({
  platform: process.env.VUE_APP_PLATFORM,
  routes: [...ROUTES]
});
// uni.navigateTo()
Vue.prototype.$navigateTo = function (path = '/') {
  typeof path == 'string' ? router.push(path) : router.push({
    path: path.path,
    query: encodeURIComponent(JSON.stringify(path.query))
  })
}
// uni.redirectTo()
Vue.prototype.$redirectTo = function (path = '/') {
  router.replace(path)
}
// uni.reLaunch()
Vue.prototype.$reLaunch = function (path = '/') {
  router.replaceAll(path)
}
// uni.switchTab()
Vue.prototype.$switchTab = function (path = '/') {
  router.pushTab(path)
}
// uni.navigateBack()
Vue.prototype.$navigateBack = function (number = '1') {
  router.back(number)
}

//全局路由前置守卫
router.beforeEach((to, from, next) => {
  next();
});
// 全局路由后置守卫
router.afterEach((to, from) => { })

export {
  router,
  RouterMount
}